package banwood.com.xiongfasy.activity;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import banwood.com.xiongfasy.BASE.BaseActivity;
import banwood.com.xiongfasy.R;
import banwood.com.xiongfasy.utils.ToolsUtil;
import butterknife.BindView;
import butterknife.OnClick;
import cn.shellinfo.serv.comm.ParamMap;
import cn.weipass.pos.sdk.IPrint;
import cn.weipass.pos.sdk.LatticePrinter;
import cn.weipass.pos.sdk.Weipos;
import cn.weipass.pos.sdk.impl.WeiposImpl;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

public class RefundSucActivity extends BaseActivity {
    @BindView(R.id.bianhao)
    TextView bianhao;
    @BindView(R.id.liushui)
    TextView liushui;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.bill_money)
    TextView billMoney;
    @BindView(R.id.print_piao)
    Button printPiao;
    private ParamMap paramMap;

    @Override
    protected int getLayoutId() {
        return R.layout.bill_detail;
    }

    @Override
    protected void initView() {
        settitle("退款成功");
        paramMap= (ParamMap) getIntent().getExtras().getSerializable("param");

    }

    @Override
    protected void initData() {
        bianhao.setText(MyApplication.getUserName());
        liushui.setText(paramMap.getString("iref",""));
        time.setText(paramMap.getString("time",""));
        billMoney.setText(paramMap.getString("money","")+"元");

    }


    @OnClick(R.id.print_piao)
    public void printOnclick(View v){
        printPiao.setClickable(false);
        WeiposImpl.as().init(RefundSucActivity.this, new Weipos.OnInitListener() {
            @Override
            public void onInitOk() {
                LatticePrinter printer = WeiposImpl.as().openLatticePrinter();
                String mediumSpline = "";
                for (int i = 0; i < ToolsUtil.mediumSize; i++) {
                    mediumSpline += "-";
                }
                String biaoti="山东禹诺智能科技有限公司";
                int sizeTitle = ToolsUtil.largeSize - ToolsUtil.length(biaoti);
                // 文字居中需要在前面补足相应空格，后面可以用换行符换行
                String titleStr = getBlankBySize((int) (sizeTitle / 2d)) + biaoti;
                printer.printText(titleStr + "\n", LatticePrinter.FontFamily.SONG,
                        LatticePrinter.FontSize.LARGE, LatticePrinter.FontStyle.BOLD);

                printer.feed(1);

                String shophao="商户号：103881511100262";
                int sizeWebUrl = ToolsUtil.mediumSize - ToolsUtil.length(shophao);
                String webUrlStr = getBlankBySize((int) (sizeWebUrl / 2d)) + shophao;
                printer.printText(webUrlStr + "\n", LatticePrinter.FontFamily.SONG,
                        LatticePrinter.FontSize.MEDIUM, LatticePrinter.FontStyle.BOLD);

                int feedCount = 1;
                //进纸数，1代表进一行的高度
                printer.feed(feedCount);

                String tuikuan="退款";
                int tuisize = ToolsUtil.mediumSize - ToolsUtil.length(tuikuan);
                String tuikuaiStr = getBlankBySize((int) (tuisize / 2d)) + tuikuan;
                printer.printText(tuikuaiStr + "\n", LatticePrinter.FontFamily.SONG,
                        LatticePrinter.FontSize.MEDIUM, LatticePrinter.FontStyle.BOLD);
                printer.feed(feedCount);
                // 打印分割线
                printer.printText(mediumSpline + "\n", LatticePrinter.FontFamily.SONG,
                        LatticePrinter.FontSize.MEDIUM, LatticePrinter.FontStyle.BOLD);
                printer.feed(feedCount);

                String money="交易金额:"+paramMap.getString("money","")+"元";
                printer.printText(money + "\n", LatticePrinter.FontFamily.SONG,
                        LatticePrinter.FontSize.LARGE, LatticePrinter.FontStyle.BOLD);

                printer.feed(1);

                String time="交易日期:"+paramMap.getString("time","");
                printer.printText(time + "\n", LatticePrinter.FontFamily.SONG, LatticePrinter.FontSize.MEDIUM,
                        LatticePrinter.FontStyle.BOLD);

                printer.feed(1);

                String liiushuihao="订单号:"+paramMap.getString("newOrderNo","");
                printer.printText(liiushuihao + "\n", LatticePrinter.FontFamily.SONG, LatticePrinter.FontSize.MEDIUM,
                        LatticePrinter.FontStyle.BOLD);

                printer.feed(1);

                String caozuoyuan="操作员:"+MyApplication.getUserName();
                printer.printText(caozuoyuan + "\n", LatticePrinter.FontFamily.SONG, LatticePrinter.FontSize.MEDIUM,
                        LatticePrinter.FontStyle.BOLD);

                //最后进纸5行,方便撕纸
                printer.feed(5);
                printer.submitPrint();

                printer.setOnEventListener(new IPrint.OnEventListener() {
                    @Override
                    public void onEvent(int i, String s) {
                        String message=BillDetailActivity.getPrintErrorInfo(i,s);
                        Toast.makeText(RefundSucActivity.this,message,Toast.LENGTH_SHORT).show();
                        printPiao.setClickable(true);
                    }
                });

            }



            @Override
            public void onError(String s) {

                Toast.makeText(RefundSucActivity.this,s,Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onDestroy() {

            }
        });

    }


    public static String getBlankBySize(int size) {
        String resultStr = "";
        for (int i = 0; i < size; i++) {
            resultStr += " ";
        }
        return resultStr;
    }


}
